﻿using DG.Tweening;
using UnityEngine;

namespace Gp.Scripts.Core {
    public class WeaponProjectileFx : MonoBehaviour {
        public const float ProjectileFlyTime = 0.02f;
        
        public GameObject hitEffect;
        public GameObject muzzle;
        public bool isInjector = false;
        public bool destroyOnHit = true;


        public void Shoot(Vector3 source, Vector3 target) {
            var dir = new Vector3(target.x - source.x, 0, 0);
            transform.forward = dir;
            transform.position = source;
            
            
            if (muzzle) {
                var o = Instantiate(muzzle);
                o.transform.forward = dir;
                o.transform.position = source;
            }
            
            
            if (isInjector) return;
            float fixDis = .25f;
            if (target.x - source.x > 0) fixDis = -fixDis;
            this.transform.DOMoveX(target.x + fixDis, ProjectileFlyTime)
                .OnComplete(() => {
                    if (this.hitEffect != null)
                        Instantiate(this.hitEffect, this.transform.position, this.transform.rotation);
                    if (destroyOnHit) Destroy(this.gameObject);
                });
        }
    }
}